package offer;

/**
 * @Author SSK
 * @Date Created in 22:09 2019/8/6
 * @Description 链表中环的入口结点
 * @Version 1.0
 */
public class T56 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
         if (pHead==null) {
             return null;
         }
         ListNode slow = pHead;
         ListNode fast = pHead;
         if (pHead.next!=null&&pHead.next.next!=null) {
             while (fast.next!=null&&fast.next.next!=null) {
                 slow = slow.next;
                 fast = fast.next.next;
                 if (slow==fast) {
                       fast = pHead;
                       while (fast!=slow) {
                           fast = fast.next;
                           slow = slow.next;
                       }
                       return slow;
                 }
             }
             return null;

         }else {
             return null;
         }
    }
}
